1264E - Beautiful League - CodeForces Solution


constructive algorithms flows graph matchings *2700

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000010,inf=0x3f3f3f3f;
namespace WFFL{
int Fir[maxn],Nxt[maxn],Too[maxn],f[maxn],tot=1,w[maxn],dis[maxn];
int S=maxn-1,T=maxn-2,cur[maxn];
queue<int> q;bool vis[maxn];
void add(int a,int b,int c,int d){
Too[++tot]=b;f[tot]=c;w[tot]= d;Nxt[tot]=Fir[a];Fir[a]=tot;
Too[++tot]=a;f[tot]=0;w[tot]=-d;Nxt[tot]=Fir[b];Fir[b]=tot;
}
bool spfa(){
memset(dis,0x3f,sizeof(dis));
q.push(S);dis[S]=0;cur[S]=Fir[S];
while(!q.empty()){
int u=q.front();q.pop();vis[u]=0;
for(int i=Fir[u];i;i=Nxt[i]){
int v=Too[i];
if(f[i]&&dis[v]>dis[u]+w[i]){
dis[v]=dis[u]+w[i];
cur[v]=Fir[v];
if(!vis[v]){q.push(v);vis[v]=1;}
}
}
}
return dis[T]!=inf;
}
int find(int u,int limit){
if(u==T)return limit;
vis[u]=1;
int flow=0;
for(int &i=cur[u];i;i=Nxt[i]){
int v=Too[i];
if(f[i]&&!vis[v]&&dis[v]==dis[u]+w[i]){
int t=find(v,min(f[i],limit-flow));
if(t)f[i]-=t,f[i^1]+=t,flow+=t;
else dis[v]=-inf;
}
if(flow==limit)break;
}
vis[u]=0;
return flow;
}
int dinic(){
int cost=0;
while(spfa())cost+=dis[T]*find(S,inf);
return cost;
}
}
int n,m,G[52][52],cd[52],id[52][52];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i){
int a,b;
scanf("%d%d",&a,&b);
G[a][b]=1;
}
int cnt=n;
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j){
++cnt;
WFFL::add(WFFL::S,cnt,1,0);
if(!G[j][i]){
WFFL::add(cnt,i,1,0);
id[i][j]=WFFL::tot;
}
if(!G[i][j]){
WFFL::add(cnt,j,1,0);
id[j][i]=WFFL::tot;
}
}
for(int i=1;i<=n;++i)
for(int j=cd[i];j<n;++j)
WFFL::add(i,WFFL::T,1,j);
WFFL::dinic();
for(int i=1;i<=n;++i,puts(""))
for(int j=1;j<=n;++j)
printf("%d",WFFL::f[id[i][j]]);
return 0;
}


Comments

Submit
0 Comments
More Questions

1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice